package com.mj.haier.calc.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.calc.domain.AzFinSettleCars;
import com.mj.haier.calc.repository.AzFinSettleCarsRepository;
import com.mj.haier.calc.repository.search.AzFinSettleCarsSearchRepository;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import com.mj.haier.calc.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing AzFinSettleCars.
 */
@RestController
@RequestMapping("/api")
public class AzFinSettleCarsResource {

    private final Logger log = LoggerFactory.getLogger(AzFinSettleCarsResource.class);

    private static final String ENTITY_NAME = "microCalcAzFinSettleCars";

    private final AzFinSettleCarsRepository azFinSettleCarsRepository;

    private final AzFinSettleCarsSearchRepository azFinSettleCarsSearchRepository;

    public AzFinSettleCarsResource(AzFinSettleCarsRepository azFinSettleCarsRepository, AzFinSettleCarsSearchRepository azFinSettleCarsSearchRepository) {
        this.azFinSettleCarsRepository = azFinSettleCarsRepository;
        this.azFinSettleCarsSearchRepository = azFinSettleCarsSearchRepository;
    }

    /**
     * POST  /az-fin-settle-cars : Create a new azFinSettleCars.
     *
     * @param azFinSettleCars the azFinSettleCars to create
     * @return the ResponseEntity with status 201 (Created) and with body the new azFinSettleCars, or with status 400 (Bad Request) if the azFinSettleCars has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/az-fin-settle-cars")
    @Timed
    public ResponseEntity<AzFinSettleCars> createAzFinSettleCars(@Valid @RequestBody AzFinSettleCars azFinSettleCars) throws URISyntaxException {
        log.debug("REST request to save AzFinSettleCars : {}", azFinSettleCars);
        if (azFinSettleCars.getId() != null) {
            throw new BadRequestAlertException("A new azFinSettleCars cannot already have an ID", ENTITY_NAME, "idexists");
        }
        AzFinSettleCars result = azFinSettleCarsRepository.save(azFinSettleCars);
        azFinSettleCarsSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/az-fin-settle-cars/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /az-fin-settle-cars : Updates an existing azFinSettleCars.
     *
     * @param azFinSettleCars the azFinSettleCars to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated azFinSettleCars,
     * or with status 400 (Bad Request) if the azFinSettleCars is not valid,
     * or with status 500 (Internal Server Error) if the azFinSettleCars couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/az-fin-settle-cars")
    @Timed
    public ResponseEntity<AzFinSettleCars> updateAzFinSettleCars(@Valid @RequestBody AzFinSettleCars azFinSettleCars) throws URISyntaxException {
        log.debug("REST request to update AzFinSettleCars : {}", azFinSettleCars);
        if (azFinSettleCars.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        AzFinSettleCars result = azFinSettleCarsRepository.save(azFinSettleCars);
        azFinSettleCarsSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, azFinSettleCars.getId().toString()))
            .body(result);
    }

    /**
     * GET  /az-fin-settle-cars : get all the azFinSettleCars.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of azFinSettleCars in body
     */
    @GetMapping("/az-fin-settle-cars")
    @Timed
    public ResponseEntity<List<AzFinSettleCars>> getAllAzFinSettleCars(Pageable pageable) {
        log.debug("REST request to get a page of AzFinSettleCars");
        Page<AzFinSettleCars> page = azFinSettleCarsRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/az-fin-settle-cars");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /az-fin-settle-cars/:id : get the "id" azFinSettleCars.
     *
     * @param id the id of the azFinSettleCars to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the azFinSettleCars, or with status 404 (Not Found)
     */
    @GetMapping("/az-fin-settle-cars/{id}")
    @Timed
    public ResponseEntity<AzFinSettleCars> getAzFinSettleCars(@PathVariable Long id) {
        log.debug("REST request to get AzFinSettleCars : {}", id);
        Optional<AzFinSettleCars> azFinSettleCars = azFinSettleCarsRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(azFinSettleCars);
    }

    /**
     * DELETE  /az-fin-settle-cars/:id : delete the "id" azFinSettleCars.
     *
     * @param id the id of the azFinSettleCars to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/az-fin-settle-cars/{id}")
    @Timed
    public ResponseEntity<Void> deleteAzFinSettleCars(@PathVariable Long id) {
        log.debug("REST request to delete AzFinSettleCars : {}", id);

        azFinSettleCarsRepository.deleteById(id);
        azFinSettleCarsSearchRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/az-fin-settle-cars?query=:query : search for the azFinSettleCars corresponding
     * to the query.
     *
     * @param query the query of the azFinSettleCars search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/az-fin-settle-cars")
    @Timed
    public ResponseEntity<List<AzFinSettleCars>> searchAzFinSettleCars(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of AzFinSettleCars for query {}", query);
        Page<AzFinSettleCars> page = azFinSettleCarsSearchRepository.search(queryStringQuery(query), pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/az-fin-settle-cars");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
